package hxy.bytecode.javassit.jar;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.NotFoundException;
import org.junit.Test;

import java.lang.reflect.InvocationTargetException;

public class CrackRedisIdeaPlugin331 {
    public ClassPool pool = ClassPool.getDefault();

    @Test
    public void test() throws NotFoundException, ClassNotFoundException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {

//        String pathName = "/home/eric/Project/Java/eric/ByteCode/build/libs/study-1.0-SNAPSHOT.jar";
        String pathName = "/home/eric/hxy/bytecode/App1.class";
        pool.insertClassPath(pathName);
        // 下面这个地方加载的是main下面包的App程序
        ClassLoader classLoader = this.getClass().getClassLoader();

        CtClass L = pool.get("App1");
//        CtClass p = pool.get("hxy.bytecode.javassit.JavassistClass");

        try {
//            CtMethod ct = L.getDeclaredMethod("hello", new CtClass[]{pool.get("java.lang.String")});
            CtMethod ct = L.getDeclaredMethod("main");

            String body = "System.out.println(\"好的\");";
            ct.setBody(body);

//            ct = p.getDeclaredMethod("f", new CtClass[]{});

//            把上面重新定义的字节码文件输出
            String directoryName = "/home/eric";
            L.writeFile(directoryName);
//            p.writeFile(directoryName);
        } catch (Exception e) {
            System.out.println("111");
            e.printStackTrace();
        }
    }
}